package com.example.mactchoperator;


import java.io.*;

import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

class ServeurTCP implements Runnable{

    public static void main(String argv[]) throws Exception {
        ServerSocket welcomeSocket = new ServerSocket(6789);
        while (true) {
        	Socket connectionSocket = welcomeSocket.accept();
        	ServeurTCP serveur = new ServeurTCP(connectionSocket);
        	Thread t = new Thread(serveur);
        	t.start();
        }
    }
    private Socket connectionSocket;
    
    public ServeurTCP(Socket client){
    	connectionSocket = client;
    }

	@Override
	public void run() {
		while(connectionSocket.isConnected()){
            try {
                BufferedReader inFromClient
                        = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                String clientSentence = inFromClient.readLine();
                if(clientSentence == null){
                	return;
                }
                System.out.println("Received: " + clientSentence);
                outToClient.writeBytes(clientSentence + "\n");
            } catch (Exception e) {
               // Logger.getLogger(ClientTCP.class.getName()).log(Level.SEVERE, null, e);
            	return;
            }
    	}
	}
}
